package com.lh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lh.client.EduClient;
import com.lh.client.UcenterClient;
import com.lh.commonutils.R;
import com.lh.commonutils.ordervo.CourseWebVoOrder;
import com.lh.commonutils.ordervo.UcenterMemberOrder;
import com.lh.entity.TOrder;
import com.lh.dao.TOrderMapper;
import com.lh.entity.vo.OrderQuery;
import com.lh.service.ITOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lh.utils.OrderNoUtil;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author lh
 * @since 2021-06-16
 */
@Service
public class TOrderServiceImpl extends ServiceImpl<TOrderMapper, TOrder> implements ITOrderService {


    @Autowired
    private EduClient eduClient;

    @Autowired
    private UcenterClient ucenterClient;

    @Resource
    private TOrderMapper tOrderMapper;

//    生成订单的方法
    @Override
    public String creatOrders(String courseId, String memberIdByJwtToken) {
//        通过远程调用根据用户id获取用户信息
        UcenterMemberOrder memberOrder = ucenterClient.getUserInfoOrser(memberIdByJwtToken);

//        通过远程调用根据课程id获取课程信息
        CourseWebVoOrder courseInfoOrder = eduClient.getCourseInfoOrder(courseId);
//       创建order对象  设置需要的数据

        TOrder order = new TOrder();
        order.setOrderNo(OrderNoUtil.getOrderNo());
        order.setCourseId(courseId);
        order.setCourseTitle(courseInfoOrder.getTitle());
        order.setCourseCover(courseInfoOrder.getCover());
        order.setTeacherName(courseInfoOrder.getTeacherName());
        order.setTotalFee(courseInfoOrder.getPrice());
        order.setMemberId(memberIdByJwtToken);
        order.setMobile(memberOrder.getMobile());
        order.setNickname(memberOrder.getNickname());
        order.setStatus(0);//支付状态
        order.setPayType(1);//支付类型
        baseMapper.insert(order);

//                返回订单号
        return order.getOrderNo();
    }

    /**
     * 查询订单
     *
     * @param page
     * @param limit
     * @param orderQuery
     * @return
     */
    @Override
    public R findAllOrder(Integer page, Integer limit, OrderQuery orderQuery) {


        LambdaQueryWrapper<TOrder> wrapper = new LambdaQueryWrapper<>();
        Page<TOrder> tOrderPage = new Page<>(page,limit);
        wrapper.orderByDesc(TOrder::getGmtModified);
        if (orderQuery==null){
            baseMapper.selectPage(tOrderPage,wrapper);
            List<TOrder> records = tOrderPage.getRecords();
            long total = tOrderPage.getTotal();
            Map map = new HashMap<>();
            map.put("total",total);
            map.put("rows",records);
            return R.ok().data(map);
        }
        String courseTitle = orderQuery.getCourseTitle();
        String teacherName = orderQuery.getTeacherName();
        String nickname = orderQuery.getNickname();
        String begin = orderQuery.getBegin();
        String end = orderQuery.getEnd();
        String memberId = orderQuery.getMemberId();
        Integer status = orderQuery.getStatus();
        if (!StringUtils.isEmpty(courseTitle)){
            wrapper.like(TOrder::getCourseTitle,courseTitle);
        }
        if (!StringUtils.isEmpty(teacherName)){
            wrapper.like(TOrder::getTeacherName,teacherName);
        }
        if (!StringUtils.isEmpty(nickname)){
            wrapper.like(TOrder::getNickname,nickname);
        }
        if (!StringUtils.isEmpty(begin)) {
            wrapper.ge(TOrder::getGmtModified, begin);
        }

        if (!StringUtils.isEmpty(end)) {
            wrapper.le(TOrder::getGmtModified, end);
        }
        if (!StringUtils.isEmpty(memberId)){
            wrapper.eq(TOrder::getMemberId,memberId);
        }
        if (!StringUtils.isEmpty(status)){
            wrapper.eq(TOrder::getStatus,status);
        }

        baseMapper.selectPage(tOrderPage,wrapper);
        List<TOrder> records = tOrderPage.getRecords();
        long total = tOrderPage.getTotal();
        Map map = new HashMap<>();
        map.put("total",total);
        map.put("rows",records);
        return R.ok().data(map);

    }

    /**
     * 分润列表
     *
     * @return
     * @param page
     * @param limit
     * @param
     */
    @Override
    public R moneymoneyList(Integer page, Integer limit,String teacherName) {

        Page<TOrder> tOrderPage = new Page<>(page,limit);


        List<TOrder> list= tOrderMapper.moneymoneyList(tOrderPage,teacherName);
        if (list.size()>0){
            Integer total = list.size();
            return R.ok().data("list",list).data("total",total);
        }
        return R.ok().data("list",list).data("total",0);

    }

    /**
     * 记录每天的订单数
     *
     * @param day
     * @return
     */
    @Override
    public Integer countOrder(String day) {
        Integer countOrder= baseMapper.countOrder(day);
        return countOrder;
    }
}
